package com.bwie.controller;

import cn.hutool.core.util.RandomUtil;
import com.bwie.service.TUserService;
import com.bwie.service.TbUserService;
import com.bwie.utils.KaptchaUtils;
import com.bwie.utils.R;
import com.bwie.vo.LoginInfoVo;
import com.bwie.vo.PageInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

/**
 * description:
 *
 * @Author caoshuaifeng
 * @Create 2025/4/22 19:49
 * @Version 1.0
 */
@RestController
@RequestMapping("/user/index")
public class IndexController {


    @Autowired
    private TbUserService tbUserService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    HttpServletResponse response;

    @Autowired
    TUserService tUserService;

    @GetMapping("/code")
    public void code(){
        //生成4位数字验证码
        String code = RandomUtil.randomNumbers(4);
        //将验证码存入redis中,
        String key="code-"+code;
        stringRedisTemplate.opsForValue().set(key,code,60, TimeUnit.SECONDS);
        //使用工具类生成图片验证码
        KaptchaUtils.generateCaptcha(response,code);

    }

    /**
     * 发送短信验证码
     * @param phone
     * @return
     */
    @PostMapping("/sendCode/{phone}")
    public R sendCode(@PathVariable("phone") String phone){
        return  tbUserService.sendCode(phone);

    }

    /**
     * 登录
     * @param loginInfoVo
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody LoginInfoVo loginInfoVo){
        return tbUserService.login(loginInfoVo);
    }

    @PostMapping("/phonelogin")
    public R phonelogin(@RequestBody LoginInfoVo loginInfoVo){
        return tbUserService.phonelogin(loginInfoVo);
    }

    @PostMapping("/getUserList")
    public R getUserList(@RequestBody PageInfoVo pageInfoVo){
        return tbUserService.getUserList(pageInfoVo);

    }

    @PostMapping("/getUser")
    public R getUser(){
        return tUserService.getUser();

    }





}
